package com.scale.scales.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.scale.scales.R;
import com.scale.scales.bean.FoodBean;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class IndexAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static HashMap<Integer, Boolean> isSelected;
    //数据集
    public List<FoodBean> mdatas;
    public Context context;
    private OnItemClickListener mOnItemClickListener;

    private int defItem = -1;//默认值

    public IndexAdapter(Context context, List<FoodBean> datas) {
        super();
        this.mdatas = datas;
        this.context = context;
    }


    public void notifyChange(List<FoodBean> datas, int defItem) {
        this.mdatas = datas;
        this.defItem = defItem;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_show_select, parent, false);
        return new FoodViewHolder(view);


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


//        if (isShowColse) {
//            ((VideoViewHolder) holder).add_transfer_item_close.setVisibility(View.INVISIBLE);
//        }
//
        ((FoodViewHolder) holder).show_name.setText(mdatas.get(position).getName());

//        if (defItem != -1) {
            if (defItem == position) {
                ((FoodViewHolder) holder).show_name.setBackgroundColor(context.getResources().getColor(R.color.submi_k3));
                ((FoodViewHolder) holder).show_name.setBackground(context.getResources().getDrawable(R.drawable.text_shape_noedit_enable));
            } else {
                ((FoodViewHolder) holder).show_name.setBackground(context.getResources().getDrawable(R.drawable.text_shape_noedit));
            }
//        }

//        ((VideoViewHolder) holder).add_transfer_item_demand_number.setText(mdatas.get(position).getQty() + "");
        if (mOnItemClickListener != null) {
            ((FoodViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onItemClick(((FoodViewHolder) holder).itemView, position);
                }
            });
            ((FoodViewHolder) holder).itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mOnItemClickListener.onItemLongClick(((FoodViewHolder) holder).itemView, position);
                    return false;
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return mdatas.size();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int positon);

        void onItemLongClick(View view, int positon);
    }

    public class FoodViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.show_name)
        TextView show_name;


        public FoodViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}

